package indi.zhifa.recipe.bailan.framework.auth.impl.dao.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import indi.zhifa.recipe.bailan.framework.auth.impl.dao.mapper.UserMapper;
import indi.zhifa.recipe.bailan.framework.auth.impl.dao.service.IUserDbService;
import indi.zhifa.recipe.bailan.framework.auth.impl.entity.po.UserEntity;
import indi.zhifa.recipe.bailan.framework.mysql.entity.annotations.ZfDbService;
import indi.zhifa.recipe.bailan.framework.mysql.service.impl.ZfDbServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

import java.io.Serializable;

@CacheConfig(cacheNames = "sys_user")
@ZfDbService(name = "UserEntity")
public class UserDbServiceImpl extends ZfDbServiceImpl<UserMapper, UserEntity> implements IUserDbService {

    @Cacheable(key = "#pId",condition = "#result!=null")
    @Override
    public UserEntity check(Serializable pId){
        return super.check(pId);
    }

    @CachePut(key = "#pId")
    @Override
    public UserEntity savePull(Long pId, UserEntity pEntity) {
        return super.savePull(pId,pEntity);
    }

    @CachePut(key = "#pId")
    @Override
    public UserEntity updatePull(Long pId, UserEntity pEntity){
        return super.updatePull(pId,pEntity);
    }

    @CacheEvict(key = "#pId")
    public UserEntity deleteById(Serializable pId){
        return super.deleteById(pId);
    }

    @CachePut(value = "sys_user-byName", key = "#pUserName")
    @Override
    public Long findByUserName(String pUserName) {
        LambdaQueryWrapper<UserEntity> queryWrapper = Wrappers.<UserEntity>lambdaQuery()
                .eq(UserEntity::getName,pUserName);
        UserEntity userEntity = findOne(queryWrapper);
        if(null == userEntity){
            return null;
        }
        return userEntity.getId();
    }

    @CachePut(value = "sys_user-byEmail", key = "#pEmail")
    @Override
    public Long findByEmail(String pEmail) {
        LambdaQueryWrapper<UserEntity> queryWrapper = Wrappers.<UserEntity>lambdaQuery()
                .eq(UserEntity::getEmail,pEmail);
        UserEntity userEntity = findOne(queryWrapper);
        if(null == userEntity){
            return null;
        }
        return userEntity.getId();
    }

    @CachePut(value = "sys_user-byPhone", key = "#pPhone")
    @Override
    public Long findByPhone(String pPhone) {
        LambdaQueryWrapper<UserEntity> queryWrapper = Wrappers.<UserEntity>lambdaQuery()
                .eq(UserEntity::getPhone,pPhone);
        UserEntity userEntity = findOne(queryWrapper);
        if(null == userEntity){
            return null;
        }
        return userEntity.getId();
    }

}
